unit uFrameList;

{$mode objfpc}{$H+}

interface

uses
    Classes, SysUtils, FileUtil, Forms, Controls, Grids, ExtCtrls,
    BGRABitmap, BGRABitmapTypes,
    uTypes;

type

    { TFraList }

    TFraList = class(TFrame)
        dgList: TDrawGrid;
        pnlToolbar: TPanel;
        pnlStatusbar: TPanel;
        procedure dgListDrawCell(Sender: TObject; aCol, aRow: integer; aRect: TRect;
            aState: TGridDrawState);
        procedure dgListSelectCell(Sender: TObject; aCol, aRow: integer; var CanSelect: boolean);
        procedure FrameResize(Sender: TObject);
    private
        FList: TISList;
    private
        FListStyle: TISListStyle;
        procedure SetListStyle(AValue: TISListStyle);
    protected
        procedure Init; virtual;
        procedure Done; virtual;

        procedure SetGridProps; virtual;

        // Override to draw AItem content on the ABitmap
        // Check ListStyle and AItem.State to determine how to draw item exactly
        // If AItem is nil then draw empty item
        procedure DrawItem(AItem: TISListItem; ABitmap: TBGRABitmap); abstract;
    public
        constructor Create(TheOwner: TComponent); override;
        destructor Destroy; override;
    public
        property ListStyle: TISListStyle read FListStyle write SetListStyle;
        property List: TISList read FList;
    end;

implementation

{$R *.lfm}

{ TFraList }

procedure TFraList.dgListDrawCell(Sender: TObject; aCol, aRow: integer; aRect: TRect; aState: TGridDrawState);
var
    bmp: TBGRABitmap;
    itm: TISListItem;
    idx: Integer;
begin
    bmp := TBGRABitmap.Create(aRect.Width, aRect.Height, 0);

    try
        case ListStyle of
            lsList:
                idx := aRow;
            lsDetails:
                idx := aRow - 1;
            lsThumbnails:
            begin
                idx := aCol + aRow * dgList.ColCount;
                if idx >= FList.Count then
                    idx := -1;
            end
            else // Unexpected list style
                idx := -1;
        end;

        if idx > -1 then
            itm := FList.ItemByIndex[idx]
        else
            itm := nil;

        DrawItem(itm, bmp);

        bmp.Draw(dgList.Canvas, aRect);
    finally
        bmp.Free;
    end;
end;

procedure TFraList.dgListSelectCell(Sender: TObject; aCol, aRow: integer; var CanSelect: boolean);
begin

end;

procedure TFraList.FrameResize(Sender: TObject);
begin
    // Set grid measurements
end;

procedure TFraList.SetListStyle(AValue: TISListStyle);
begin
    if FListStyle = AValue then
        Exit;
    FListStyle := AValue;
end;

procedure TFraList.Init;
begin
    // UI
    // =====================================================
    pnlToolbar.Height := 22;
    pnlStatusbar.Height := 22;

    dgList.FixedCols := 0;
end;

procedure TFraList.Done;
begin

end;

procedure TFraList.SetGridProps;
begin
    case FListStyle of
        lsList:
        begin
            dgList.FixedRows := 0;
            dgList.DefaultRowHeight := 16 + 12 + 20 + 2 + 6;
        end;
        lsDetails:
        begin
            dgList.FixedRows := 1;
            dgList.DefaultRowHeight := 19;
        end;
        lsThumbnails:
        begin
            dgList.FixedRows := 0;
            dgList.DefaultRowHeight := 128;
        end;
    end;
end;

constructor TFraList.Create(TheOwner: TComponent);
begin
    inherited Create(TheOwner);

    Init;
end;

destructor TFraList.Destroy;
begin
    Done;

    inherited Destroy;
end;

end.



